package com.zx.Ding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.Ding.common.CustomException;
import com.zx.Ding.dto.DishDto;
import com.zx.Ding.dto.SetmealDto;
import com.zx.Ding.entity.Setmeal;
import com.zx.Ding.entity.SetmealDish;
import com.zx.Ding.mapper.SetmealMapper;
import com.zx.Ding.service.DishService;
import com.zx.Ding.service.SetmealDishService;
import com.zx.Ding.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐,同时需要保存套餐和菜品的关系  dish_id
     * @param setmealDto
     */
    /*加入事务注解 保证事务的一致性*/
    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息,操作setmeal,执行insert操作
        this.save(setmealDto);

        //取出套餐中的每一个菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品关联的信息,
        setmealDishService.saveBatch(setmealDishes);
    }
    /*
     根据套餐ID查询对应的菜品信息和口味信息
    * */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //从分类表中获取基本信息
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //查询套餐当中对应的菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(setmealDishes);


        return setmealDto;
    }

    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //更新setmeal表基本信息
        this.updateById(setmealDto);
        //请理当前套餐对应的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //添加对应的菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes=setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

    }


    /**
     * 删除套餐
     * @param ids
     */

    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {
        //查询套餐状态 确定是否可用删除
        //sql 语句 select Count(*) from setmeal where id in (1,2,3) and stastus=1
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(queryWrapper);
        if (count>0){
            //如果不能删除  抛出一个业务异常
            throw new CustomException("套餐正在售卖中 不能删除");
        }
        //如果可以删除 删除套餐中的数据
        this.removeByIds(ids);

        //删除关系表中的数据
        List<Setmeal> setmeals = this.listByIds(ids);

        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealDish::getSetmealId,ids);

        setmealDishService.remove(queryWrapper1);


    }
}
